import java.util.Arrays;

// 定义协议数据结构
class ProtocolPacket {
    // 帧头 (2字节)
    short frameHeader;
    // 帧类型 (1字节)
    byte frameType;
    // 数据帧长度 (2字节)
    short dataFrameLength;
    // 采集设备编号 (8字节)
    long deviceSN;
    // 时间戳 (8字节)
    long timestamp;
    // 数据项数量 (1字节)
    byte dataItemCount;
    // 数据项内容 (N * 10字节)
    byte[] dataItems;
    // 滚动校验 (1字节)
    byte rollingCheck;
    // 累加校验 (1字节)
    byte checksum;

    public ProtocolPacket(short frameHeader, byte frameType, short dataFrameLength,
                          long deviceSN, long timestamp, byte dataItemCount,
                          byte[] dataItems, byte rollingCheck) {
        this.frameHeader = frameHeader;
        this.frameType = frameType;
        this.dataFrameLength = dataFrameLength;
        this.deviceSN = deviceSN;
        this.timestamp = timestamp;
        this.dataItemCount = dataItemCount;
        this.dataItems = dataItems;
        this.rollingCheck = rollingCheck;
        this.checksum = 0;
    }
}

public class PacketGenerator {

    // 生成协议数据包
    public static int generatePacket(ProtocolPacket packet, byte[] buffer) {
        int offset = 0;

        // 写入帧头 (小端模式)
        buffer[offset++] = (byte) (packet.frameHeader & 0xFF);
        buffer[offset++] = (byte) ((packet.frameHeader >> 8) & 0xFF);

        // 写入帧类型
        buffer[offset++] = packet.frameType;

        // 写入数据帧长度 (小端模式)
        buffer[offset++] = (byte) (packet.dataFrameLength & 0xFF);
        buffer[offset++] = (byte) ((packet.dataFrameLength >> 8) & 0xFF);

        // 写入采集设备编号 (小端模式)
        for (int i = 0; i < 8; i++) {
            buffer[offset++] = (byte) ((packet.deviceSN >> (8 * i)) & 0xFF);
        }

        // 写入时间戳 (小端模式)
        for (int i = 0; i < 8; i++) {
            buffer[offset++] = (byte) ((packet.timestamp >> (8 * i)) & 0xFF);
        }

        // 写入数据项数量
        buffer[offset++] = packet.dataItemCount;

        // 写入数据项内容
        System.arraycopy(packet.dataItems, 0, buffer, offset, packet.dataItemCount * 10);
        offset += packet.dataItemCount * 10;

        // 写入滚动校验
        buffer[offset++] = packet.rollingCheck;

        // 计算并写入累加校验
        byte checksum = 0;
        for (int i = 0; i < offset; i++) {
            checksum += buffer[i];
        }
        buffer[offset++] = checksum;

        // 添加调试信息
        System.out.printf("Generated data length: %d\n", offset);

        return offset;
    }

    // 示例：打包数据
    public static void packExample() {
        byte[] dataItems = {
            (byte) 0x44, (byte) 0x2F, (byte) 0xE1, (byte) 0xA4, (byte) 0x5C, (byte) 0x97, (byte) 0x52, (byte) 0x24, (byte) 0x01, (byte) 0x00, // 数据项1
            (byte) 0x45, (byte) 0x2F, (byte) 0x71, (byte) 0x3D, (byte) 0x52, (byte) 0x41, (byte) 0x47, (byte) 0x2F, (byte) 0x00, (byte) 0x00  // 数据项2
        };

        ProtocolPacket packet = new ProtocolPacket(
                (short) 0x5753, // 帧头
                (byte) 0x01,    // 帧类型
                (short) 63,     // 数据帧长度
                610100122060001L, // 采集设备编号
                0L,             // 时间戳
                (byte) 2,       // 数据项数量
                dataItems,
                (byte) 198      // 滚动校验
        );

        byte[] buffer = new byte[1024];
        int bufferLen = generatePacket(packet, buffer);

        // 打印生成的数据包
        System.out.println("Generated packet:");
        for (int i = 0; i < bufferLen; i++) {
            System.out.printf("%02X ", buffer[i]);
        }
        System.out.println();
    }

    public static void main(String[] args) {
        packExample();
    }
}